package com.gxuwz.attend.web.servlet;

import com.gxuwz.attend.context.AppContext;
import com.gxuwz.attend.entity.User;
import com.gxuwz.attend.service.UserService;
import com.gxuwz.attend.tools.MD5Util;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;

import java.io.IOException;
import java.util.List;

/**
 * 用户管理控制器（页面控制器）
 * 注意：页面控制器是负责指特定动作处理请求的Servlet对象,一般地，
 * MIS(信息系统)的一个模块(module)包括处理CRUD等类型的动作请求。
 *   前端控制器是负责整个站点所有请求处理的（Servlet和Filter）对象，主流的MVC框架都是基于Servlet或者Filter实现
 *   典型web MVC框架的包括:基于Servlet的springMVC和基于Filter的struts2.
 * 基于Servlet实现的页面控制器都必须继承httpServlet.通常重写doGet|doPost方法。
 * 但是要注意：考虑servlet-api的版本，比如servlet2.5、servlet 6.0等,不同的tomcat支持servlet-api的版本。
 */
@WebServlet(name = "userServlet",urlPatterns = {"/user"})
public class UserServlet extends BaseHttpServlet {
     //1:表示功能模块对应页面的变量，变量命名为view_[add|edit|list]
       private final static String VIEW_ADD="/WEB-INF/views/user/user_add.jsp";
       private final static String VIEW_EDIT="/WEB-INF/views/user/user_edit.jsp";
       private final static String VIEW_LIST="/WEB-INF/views/user/user_list.jsp";

       private final static String ACTION_LIST="/user?action=list";
     //2:在init()方法中实例化业务逻辑service组件
       private  UserService userService;
    @Override
    public void init() throws ServletException {
        super.init();
        userService= AppContext.getBean(UserService.class);
    }
    //3:重写doGet|doPost方法

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1：接受请求参数action以及其他页面的参数值，按照约定，动作请求action依次为input、add、delete、get、edit、list.
         String action =req.getParameter("action");
         String view=ErrorPage_500;
         try {
             //2:调用userService完成处理
             switch (action) {
                 case "input":
                     view = VIEW_ADD;
                     User user =new User();
                     user.setUserid("27105");
                     user.setPassword("123456");
                     user.setEmail("1007065435@qq.com");
                     req.setAttribute("user",user);
                     break;
                 case "add":
                     //接收表单(Form)参数,并转换为实体对象的属性值
                     String userid =req.getParameter("userid");
                     String fullname =req.getParameter("fullname");
                     String password =req.getParameter("password");
                     String status =req.getParameter("status");
                     String email =req.getParameter("email");
                      user =new User();
                      user.setStatus(status);
                      user.setFullname(fullname);
                      user.setPassword(MD5Util.encryptMd5(password));
                      user.setEmail(email);
                      user.setUserid(userid);
                     //调用service组件完成数据写入
                     userService.addUser(user);
                     view = ACTION_LIST;
                     break;
                 case "delete":
                     String id =req.getParameter("id");
                     userService.deleteUser(id);
                     view = ACTION_LIST;
                     break;
                 case "get":

                     view = VIEW_EDIT;
                     break;
                 case "edit":
                     view = ACTION_LIST;
                     break;
                 case "list":
                     //查找并显示所有用户信息列表
                     List<User> userList = userService.list(null);
                     view = VIEW_LIST;
                     req.setAttribute("list",userList);
                     break;
                 default:
             }
         }catch (Exception e){
             e.printStackTrace();
             req.setAttribute("message",e.getMessage());
             view =ErrorPage_500;
         }
        //3:页面跳转，默认转发方式
        super.doDispatch(req,resp,view);
    }
}
